package com.jushu.storbox.view.dialog;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

public class MyPopupWindow {

	/** context */
	private Context context;

	/** popup window instance. */
	private PopupWindow pw;
	
	/** father view of popup window */
	private View fatherView = null;

	/**
	 * MyPopupWindow Construct function
	 */
	public MyPopupWindow() {
		super();
	}

	/**
	 * MyPopupWindow Construct function
	 * 
	 * @param context
	 *            Context
	 */
	public MyPopupWindow(Context context) {
		super();
		this.context = context;
	}

	/**
	 * @param view
	 * @param width
	 * @param heigth
	 * @return popupWindow 
	 */
	public PopupWindow initPopupWindow(View view, int width, int heigth) {
		pw = new PopupWindow(view, width, heigth, true);
		pw.setOutsideTouchable(true);
		return pw;
	}
	

	public void closePopupWindow() {
		pw.dismiss();
	}
	
	public void setFatherView(View view) {
		this.fatherView = view;
	}
	
	private void setFatherViewEnable() {
		if (null != fatherView) {
			fatherView.setClickable(true);
		}
	}
	
	private void setFatherViewDisable() {
		if (null != fatherView) {
			fatherView.setClickable(false);
		}
	}

	/**
	 * show popupwindow view
	 * 
	 * @param context
	 * @param resource
	 * @param parent
	 * @param width
	 * @param heigth
	 * @return View return popupwindow view
	 */
	public synchronized View showWindowInCenter(Context context, int resource, View parent, int width, int heigth) {
		setFatherViewDisable();
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(resource, null);
		PopupWindow window = initPopupWindow(view, width, heigth);
		window.setOutsideTouchable(true);
		window.setBackgroundDrawable(new BitmapDrawable());
		window.showAtLocation(parent, Gravity.CENTER, 0, 0);
		return view;
	}

}
